<template>
  <div :id="starsId" :style="{ zIndex: zIndex }">
    <div v-for="(star, index) in stars" :key="index" :class="star.className" :style="{ top: star.top + 'px', left: star.left + 'px' }"></div>
  </div>
</template>

<script>
import { onUnmounted } from 'vue';

export default {
  name: 'MeteorShower',
  props: {
    // 定义接收父组件传入的流星数量的 prop
    meteorCount: {
      type: Number,
      default: 30
    },
    // 新增接收层级数量的 prop
    zIndexValue: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      stars: [],
      starsId: 'stars',
      animationFrameId: null
    };
  },
  computed: {
    zIndex() {
      return this.zIndexValue;
    }
  },
  mounted() {
    this.generateStars();
    this.addAnimationDelay();
    this.animationFrameId = requestAnimationFrame(this.animate);
  },
  methods: {
    generateStars() {
      const screenWidth = window.innerWidth;
      for (let j = 0; j < this.meteorCount; j++) {
        const newStar = {
          className: "star",
          top: 0,
          left: this.randomDistance(0, screenWidth)
        };
        this.stars.push(newStar);
      }
    },
    randomDistance(max, min) {
      return Math.floor(Math.random() * (max - min + 1) + min);
    },
    addAnimationDelay() {
      const starElements = document.getElementsByClassName('star');
      for (let i = 0, len = starElements.length; i < len; i++) {
        starElements[i].style.animationDelay = i % 6 === 0? '0s' : i * 0.8 + 's';
      }
    },
    animate() {
      // 这里是动画逻辑
      // 可以根据需要更新流星的位置等
      this.animationFrameId = requestAnimationFrame(this.animate);
    }
  },
  setup() {
    const vm = this;
    onUnmounted(() => {
      if (vm.animationFrameId) {
        cancelAnimationFrame(vm.animationFrameId);
      }
    });
  }
};
</script>

<style scoped>
html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: linear-gradient(rgba(0, 108, 172, 1), rgba(0, 122, 195, .7));
}

#stars {
  margin: 0 auto;
  max-width: 1600px;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.star {
  display: block;
  width: 1px;
  background: transparent;
  position: relative;
  opacity: 0;
  animation: star-fall 3s linear infinite;
  -webkit-animation: star-fall 3s linear infinite;
  -moz-animation: star-fall 3s linear infinite;
}

.star:after {
  content: '';
  display: block;
  border: 0px solid #fff;
  border-width: 0px 90px 2px 90px;
  border-color: transparent transparent transparent rgba(255, 255, 255, .5);
  box-shadow: 0 0 1px 0 rgba(255, 255, 255, .1);
  transform: rotate(-45deg) translate3d(1px, 3px, 0);
  -webkit-transform: rotate(-45deg) translate3d(1px, 3px, 0);
  -moz-transform: rotate(-45deg) translate3d(1px, 3px, 0);
  transform-origin: 0% 100%;
  -webkit-transform-origin: 0% 100%;
  -moz-transform-origin: 0% 100%;
}

@keyframes star-fall {
  0% {
    opacity: 0;
    transform: scale(0.5) translate3d(0, 0, 0);
    -webkit-transform: scale(0.5) translate3d(0, 0, 0);
    -moz-transform: scale(0.5) translate3d(0, 0, 0);
  }
  50% {
    opacity: 1;
    transform: translate3d(-200px, 200px, 0);
    -webkit-transform: translate3d(-200px, 200px, 0);
    -moz-transform: translate3d(-200px, 200px, 0);
  }
  100% {
    opacity: 0;
    transform: scale(1.2) translate3d(-300px, 300px, 0);
    -webkit-transform: scale(1.2) translate3d(-300px, 300px, 0);
    -moz-transform: scale(1.2) translate3d(-300px, 300px, 0);
  }
}
</style>